﻿using System;
using System.Text;

namespace CodeContrib
{
	public class RandomSentence : IRandom<string>
	{
		private readonly RandomWord _randomWord;
		private readonly RandomInteger _randomWordCount;

		public static int MaxWordCount { get; set; }
		public static int MinWordCount { get; set; }

		static RandomSentence()
		{
			MaxWordCount = 10;
			MinWordCount = 1;
		}

		public RandomSentence() : this(MinWordCount, MaxWordCount)
		{
		}

		public RandomSentence(int maxWordCount) : this(MinWordCount, maxWordCount)
		{
		}

		public RandomSentence(int minWordCount, int maxWordCount)
		{
			Guard.NotNegative(minWordCount, "minWordCount");
			Guard.NotNegativeOrZero(maxWordCount, "maxWordCount");

			if (minWordCount > maxWordCount)
			{
				throw new ArgumentOutOfRangeException("minWordCount", minWordCount, "'minWordCount' cannot be greater than 'maxWordCount'");
			}

			_randomWordCount = new RandomInteger(minWordCount, maxWordCount);
			_randomWord = new RandomWord();
		}

		public string Next()
		{
			var sentence = new StringBuilder();
			sentence.Append(_randomWord.Next().CharsToUpper(0));

			var wordCount = _randomWordCount.Next();

			for (var i = 0; i < wordCount - 1; i++)
			{
				sentence.Append(" ");
				sentence.Append(_randomWord.Next());
			}

			sentence.Append(".");

			return sentence.ToString();
		}
	}
}